package com.carmanagement



import static org.springframework.http.HttpStatus.*
import grails.plugin.springsecurity.annotation.Secured
import grails.transaction.Transactional


@Secured('permitAll')
@Transactional(readOnly = true)
class UserController {

    def springSecurityService
	
	def simpleCaptchaService

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    @Secured('permitAll')
    def index(){
		User user = springSecurityService.getCurrentUser()
	
		if (user?.isAdmin()){
		    redirect controller : "user", action : "indexAdmin"
		}else if (user){
		    redirect controller : "vehicle", action : "index"
		}
    }

    @Secured('ROLE_ADMIN')
    def indexAdmin(Integer max) {
		params.max = Math.min(max ?: 10, 100)
		respond User.list(params), model:[userInstanceCount: User.count()]
    }

    @Secured('IS_AUTHENTICATED_FULLY')
    def show(User userInstance) {
		respond userInstance
    }

    @Secured('permitAll')
    def create() {
		respond new User(params)
    }

    @Transactional
    @Secured('permitAll')
    def save(User userInstance) {
		if (userInstance == null) {
		    notFound()
		    return
		}
		if(params?.passwordConfirm != userInstance.password){
			userInstance.errors.rejectValue ("password", 'user.passwords.different')
		}
		if(!simpleCaptchaService.validateCaptcha(params.captcha)){
			userInstance.errors.rejectValue ("password", 'user.captcha.wrong')
		}
		if (userInstance.hasErrors()) {
		    respond userInstance.errors, view:'create'
		    return
		}
	
		userInstance.save flush:true
		def userRole = Role.findByAuthority(RoleEnum.USER_ROLE.value)
		new UserRole(user:userInstance, role:userRole).save(flush : true)
		
		springSecurityService.reauthenticate(userInstance.username)
	
		request.withFormat {
		    form {
			flash.message = message(code: 'default.created.message', args: [
			    message(code: 'userInstance.label', default: 'User'),
			    userInstance.id
			])
			redirect controller : "vehicle", action : "index"
		    }
		    '*' { respond userInstance, [status: CREATED] }
		}
    }

    @Secured('IS_AUTHENTICATED_FULLY')
    def edit(User userInstance) {
		respond userInstance
    }

    @Transactional
    @Secured('IS_AUTHENTICATED_FULLY')
    def update(User userInstance) {
		if (userInstance == null) {
		    notFound()
		    return
		}
	
		if (userInstance.hasErrors()) {
		    respond userInstance.errors, view:'edit'
		    return
		}
	
		userInstance.save flush:true
	
		request.withFormat {
		    form {
			flash.message = message(code: 'default.updated.message', args: [
			    message(code: 'User.label', default: 'User'),
			    userInstance.id
			])
			redirect userInstance
		    }
		    '*'{ respond userInstance, [status: OK] }
		}
    }

    @Transactional
    @Secured('ROLE_ADMIN')
    def delete(User userInstance) {

		if (userInstance == null) {
		    notFound()
		    return
		}
	
		userInstance.delete flush:true
	
		request.withFormat {
		    form {
			flash.message = message(code: 'default.deleted.message', args: [
			    message(code: 'User.label', default: 'User'),
			    userInstance.id
			])
			redirect action:"index", method:"GET"
		    }
		    '*'{ render status: NO_CONTENT }
		}
    }

    protected void notFound() {
		request.withFormat {
		    form {
			flash.message = message(code: 'default.not.found.message', args: [
			    message(code: 'userInstance.label', default: 'User'),
			    params.id
			])
			redirect action: "index", method: "GET"
		    }
		    '*'{ render status: NOT_FOUND }
		}
    }
}